// pages/index/game/game.js
var figure = require("../../../utils/figure.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {

    // rowStyle:"width:700rpx;height:160rpx;margin:5rpx;",
    // colStyle: "width:160rpx;float:left;height:160rpx;border:1px solid #cccoverflow: hidden;background: url('../../image/2.jpg') no-repeat;transition: all 0.5s ease 0s;",
    picBox: "margin:30rpx auto;width:630rpx;height:630rpx;",
    ImageSrc:"",
    pic:[],
    pic1: [],
    isFirst:true,      //判断是不是首次点击这个方块，需不需要更换位置，
    tempAddr:-1,
    currentNum:0,
    originalHeight: "",
    originalWidth: ""

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
  var that = this;
  var temp = figure.figure(options.originalWidth, options.originalHeight,3);
    console.log(temp)
    that.setData({
      pic:temp,
      pic1:temp,
      ImageSrc:options.addpic,
      originalHeight: options.originalHeight,
      originalWidth: options.originalWidth
    })
    // that.widthHeight(options.addpic)
  },
  widthHeight: function (e) {
    wx.getImageInfo({
      src: e,
      success:function(res){
        console.log(res)
        
      }
    })
    // console.log("执行了widthHeight")
    // let originalWidth = e.detail.width;
    // let originalHeight = e.detail.height;
    // console.log(originalWidth);
    // console.log(originalHeight)
  },

  // 设置不同的拼图小块数据；
  // setNum:function(e){
  //   var that = this;
  //   var num = e.currentTarget.dataset.num;
  //   console.log(num)
  //   var temp = figure.figure(480,480, num);
  //   that.setData({
  //     currentNum:num,
  //     pic: temp,
  //     pic1: temp
  //   })
  // },
  // 拼图乱序
  go:function(){
    var that = this;
    that.data.pic.sort(function (a, b) { return Math.random() > .5 ? -1 : 1; });
    that.setData({
      pic:that.data.pic,
    })
  },
  // 拼图模块更换位置
  touchD:function(e){
    var that = this;
    console.log(e.currentTarget.dataset.index)
    that.setData({
      isFirst:false,
      tempAddr: e.currentTarget.dataset.index,
    })
  },
  touchH:function(e){
    var that = this;
    console.log("更换位置")
    console.log(e.currentTarget.dataset.index)
    console.log(that.data.pic[e.currentTarget.dataset.index]);
    console.log(that.data.pic[that.data.tempAddr])
    var temp = that.data.pic[e.currentTarget.dataset.index];
    that.data.pic[e.currentTarget.dataset.index] = that.data.pic[that.data.tempAddr];
    that.data.pic[that.data.tempAddr]=temp;

    console.log(that.data.pic)
    that.setData({
      isFirst:true,

      pic:that.data.pic,
    })
    that.isSuccess();
  },
  // 判断拼图是否完成；
  isSuccess:function(){
    var that = this;
    var i;
    var len = that.data.pic.length;
    for(i=0;i<len;i++){
      if(that.data.pic[i].id!==that.data.pic1[i].id){
        break;
      }
    }
   if(i==len){
     wx.showToast({
       content:"游戏结束"
     })
   }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})